package com.liangzhong.modules.compete.mediator
{
	import com.common.queueloader.QueueLoader;
	import com.common.queueloader.QueueLoaderEvent;
	import com.common.queueloader.QueueVo;
	import com.common.utils.Reflection;
	import com.global.GlobalDef;
	import com.liangzhong.constants.ModulePanelTypeConst;
	import com.liangzhong.constants.NotificationConstants;
	import com.liangzhong.constants.ResourceConst;
	import com.liangzhong.events.AppEventDispatcher;
	import com.liangzhong.events.CompeteEvent;
	import com.liangzhong.events.ModuleEvent;
	import com.liangzhong.manager.ResourceManager;
	import com.liangzhong.mediator.ModuleMediator;
	import com.liangzhong.model.AppData;
	import com.liangzhong.proxyconstant.compete.CompeteProxyConstant;
	import com.lz.football.module.base.IModuleView;
	import com.lz.football.module.compete.ICompete;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	
	import org.puremvc.as3.interfaces.INotification;
	
	public class CompeteMediator extends ModuleMediator
	{
		public static const MediatorName :String = "CompeteMediator";
		public function CompeteMediator(mediatorName:String=null, viewComponent:Object=null, param:Object=null)
		{
			super(mediatorName, viewComponent, param);
		}
		
		/**
		 *获取 实例 
		 * @return 
		 * 
		 */		
		public function get view():ICompete
		{
			return viewComponent as ICompete;
		}
		/**
		 *添加事件 
		 * 
		 */		
		override public function addEventListeners():void
		{
			AppEventDispatcher.getInstance().addEventListener(CompeteEvent.KINGSELECTCHANGELLNATION,selectchallengenation);
			(view as DisplayObject).addEventListener(CompeteEvent.KANDSENIOR,kAndsenior);
			(view as DisplayObject).addEventListener(ModuleEvent.REMOVE_MODULE,onremoveHandler);
			AppEventDispatcher.getInstance().addEventListener(CompeteEvent.CHANGELL,competeMatch);
			AppEventDispatcher.getInstance().addEventListener(CompeteEvent.BUYTREASURY,buytreasury);
			AppEventDispatcher.getInstance().addEventListener(CompeteEvent.DONATE,onDONATEHandler);
			AppEventDispatcher.getInstance().addEventListener(CompeteEvent.ISSUETASK,issustask);
			AppEventDispatcher.getInstance().addEventListener(CompeteEvent.OTHER_INFO,otherInfo);
			AppEventDispatcher.getInstance().addEventListener(CompeteEvent.GETTREASURYFUNDS,onTreasuryfunds);
			
			(view as DisplayObject).addEventListener(CompeteEvent.MOREINLANDRANK,onMoreRankHandler);
		}
		
		/**
		 * 取国库资金 
		 * @param event
		 * 
		 */
		protected function onTreasuryfunds(event:CompeteEvent):void
		{
			// TODO Auto-generated method stub
			this.sendNotification(CompeteProxyConstant.COMPETE_MEDIATORTOCOMMAND,event.param,CompeteProxyConstant.GETTREASURYFUNDS);
		}
		
		/**
		 *更多的排名  
		 * @param event
		 * 
		 */
		protected function onMoreRankHandler(event:CompeteEvent):void
		{
			// TODO Auto-generated method stub
			this.sendNotification(CompeteProxyConstant.COMPETE_MEDIATORTOCOMMAND,event.param,CompeteProxyConstant.MOREINLANDRANK);
		}
		
		protected function otherInfo(event:CompeteEvent):void
		{
			// TODO Auto-generated method stub
			this.sendNotification(CompeteProxyConstant.COMPETE_MEDIATORTOCOMMAND,event.param,CompeteProxyConstant.OTHER_INFO);
		}
		
		protected function onDONATEHandler(event:CompeteEvent):void
		{
			// TODO Auto-generated method stub
			this.sendNotification(CompeteProxyConstant.COMPETE_MEDIATORTOCOMMAND,event.param,CompeteProxyConstant.DONATE);
		}
		protected function onremoveHandler(event:Event):void
		{
			// TODO Auto-generated method stub
			removeMediator();
		}
		
		/**
		 *国王和三大将 
		 * 
		 */
		private function kAndsenior(event:CompeteEvent):void
		{
			this.sendNotification(CompeteProxyConstant.COMPETE_MEDIATORTOCOMMAND,event.param,CompeteProxyConstant.KINGANDSENIOR);
		}
		/**
		 *发布任务 
		 * @param event
		 * 
		 */
		private function issustask(event:CompeteEvent):void
		{
			this.sendNotification(CompeteProxyConstant.COMPETE_MEDIATORTOCOMMAND,event.param,CompeteProxyConstant.ISSUSTASK);
		}
		
		/**
		 * 国王选择挑战国家 
		 * @param event
		 * 
		 */
		private function selectchallengenation(event:CompeteEvent):void
		{
			this.sendNotification(CompeteProxyConstant.COMPETE_MEDIATORTOCOMMAND,event.param,CompeteProxyConstant.SELECTCHALLENGENATION);
		}
		/**
		 *国家对抗赛比赛 
		 * 
		 */
		private function competeMatch(event:CompeteEvent):void
		{
			this.sendNotification(CompeteProxyConstant.COMPETE_MEDIATORTOCOMMAND,event.param,CompeteProxyConstant.COMPETEMATCH);
		}
		/**
		 *购买宝库物品 
		 * @param event
		 * 
		 */
		private function buytreasury(event:CompeteEvent):void
		{
			this.sendNotification(CompeteProxyConstant.COMPETE_MEDIATORTOCOMMAND,event.param,CompeteProxyConstant.BUYTREASURY);
		}
		/**
		 * 删除事件 
		 * 
		 */
		override public function removeEventListeners():void
		{
			AppEventDispatcher.getInstance().removeEventListener(CompeteEvent.KINGSELECTCHANGELLNATION,selectchallengenation);
			(view as DisplayObject).removeEventListener(CompeteEvent.KANDSENIOR,kAndsenior);
			(view as DisplayObject).removeEventListener(ModuleEvent.REMOVE_MODULE,onremoveHandler);
			AppEventDispatcher.getInstance().removeEventListener(CompeteEvent.CHANGELL,competeMatch);
			AppEventDispatcher.getInstance().removeEventListener(CompeteEvent.BUYTREASURY,buytreasury);
			AppEventDispatcher.getInstance().removeEventListener(CompeteEvent.DONATE,onDONATEHandler);
			AppEventDispatcher.getInstance().removeEventListener(CompeteEvent.ISSUETASK,issustask);
			AppEventDispatcher.getInstance().removeEventListener(CompeteEvent.OTHER_INFO,otherInfo);
			(view as DisplayObject).removeEventListener(CompeteEvent.MOREINLANDRANK,onMoreRankHandler);
			AppEventDispatcher.getInstance().removeEventListener(CompeteEvent.GETTREASURYFUNDS,onTreasuryfunds);
		}
		/**
		 * 监听信息
		 * @return 
		 * 
		 */		
		override public function listNotificationInterests():Array
		{
			return [CompeteProxyConstant.GETISSUSTASK,
				CompeteProxyConstant.GETKINGANDSENIOR,
				CompeteProxyConstant.SELECTCHALLENGENATION,
			CompeteProxyConstant.GETBUYTREASURY,
			CompeteProxyConstant.GETCOMPETEINFO,
			CompeteProxyConstant.GETOTHER_INFO,
			CompeteProxyConstant.GETDONATE,
			CompeteProxyConstant.GET_MATCH_RESULT,
			CompeteProxyConstant.GETMOREINLANDRANK,
			CompeteProxyConstant.GET_CHALLENGE].concat(super.listNotificationInterests());
		}
		/**
		 * 处理信息
		 * @param notification
		 * 
		 */		
		override public function handleNotification(notification:INotification):void
		{
			switch(notification.getName())
			{
				case CompeteProxyConstant.GETISSUSTASK://返回发布任务
					
					break;
				case CompeteProxyConstant.GETKINGANDSENIOR://返回国王和三大将信息
					if(view != null)view.tkingandsenior(notification.getBody());
					break;
//				case CompeteProxyConstant.SELECTCHALLENGENATION://返回国王选择挑战国家
//					
//					break;
				case CompeteProxyConstant.GETBUYTREASURY://返回购买宝库物品
					
					break;
				case CompeteProxyConstant.GETCOMPETEINFO:
					if(!ResourceManager.instance.isContainResource(ResourceConst.COMPETE_SWF))return;
					if(view == null)
					{
						showUI();
					}else
					{
						view.updataOppList();
					}
					
					break;
				case CompeteProxyConstant.GETOTHER_INFO:
					view.otherInfo(notification.getBody());
					break;
				case CompeteProxyConstant.GETDONATE:
					if(view != null)view.getdonate();
					break;
				case CompeteProxyConstant.GET_MATCH_RESULT:
					if(view != null)view.getMatchResult(notification.getBody());
					break;
				case CompeteProxyConstant.GETMOREINLANDRANK:
					if(view != null)view.moreinlandRank(notification.getBody());
					break;
				case CompeteProxyConstant.GET_CHALLENGE:
					if(view != null)view.getChallange();
					break;
			}
		}
		/**
		 * 
		 * 注册
		 */		
		override public function onRegister():void
		{
			if(AppData.getInstance().competeDataManager.competeInfoVo == null || AppData.getInstance().competeDataManager.competeInfoVo.rivalcountry == 0||
				AppData.getInstance().competeDataManager.competeInfoVo.isupdateopponentinfo )
			{
				this.sendNotification(CompeteProxyConstant.COMPETE_MEDIATORTOCOMMAND,null,CompeteProxyConstant.COMPETEINFO);
			}
			resName = ResourceConst.COMPETE_SWF;
			if(ResourceManager.instance.isContainResource(ResourceConst.COMPETE_SWF))
			{
				showUI();
			}
			else
			{
				loader = new QueueLoader();
				var clubConfig :XML = XML(ResourceManager.instance.getResource(ResourceConst.MODULE_RES_CONFIG)).compete[0];
				bytesTotal = int(clubConfig.@totalBytes);
				var releaseVersion :String;
				for each(var item :XML in clubConfig.item)
				{
					releaseVersion = "?" + item.@version + "." + item.@itemType;
					loader.addQueue(new QueueVo(item.@resName,item.@id,item.@id),item.@url + releaseVersion);
				}
				loadRes();
			}
		}
		/**
		 *加载资源完毕 
		 * @param e
		 * 
		 */		
		override protected function onLoadComplete(e:QueueLoaderEvent):void
		{
			showUI();
			super.onLoadComplete(e);
			ResourceManager.instance.putResource(ResourceConst.COMPETE_SWF,resList)
		}
		private function showUI():void
		{
			if(AppData.getInstance().competeDataManager.competeInfoVo == null)return;
			
			var cView :IModuleView = Reflection.createInstance("com.lz.football.module.compete.CompeteView");
			cView.initUI();
			viewComponent = cView;
			this.sendNotification(NotificationConstants.MAINVIEW_ADD_DISPLAYOBJECT,{type:ModulePanelTypeConst.COMPETE,dispalyObj:view.bg});
			view.updataOppList();
			GlobalDef.AlertContainer.addChild(viewComponent as DisplayObject);
			addEventListeners();
			//设置缓存位图
			setCacheAsBitmap(true);
		}
		/**
		 * 删除 mediator
		 * 
		 */		
		override public function removeMediator():void
		{
			facade.removeMediator(MediatorName);
			this.sendNotification(NotificationConstants.MAINVIEW_REMOVE_DISPLAYOBJECT);
		}
		/**
		 *删除 清理 
		 * 
		 */		
		override public function onRemove():void
		{
			super.onRemove();
			removeEventListeners();
			(viewComponent as IModuleView).destroyView();
			viewComponent = null;
		}
	}
}